package com.yunxi.service.dubboService.impl.vip;

import cn.jiguang.common.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerStorageVO;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.dealer.service.PartnerDubboService;
import com.yunxi.model.vip.Vip;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.vip.VipService;
import com.yunxi.vip.pojo.VipVO;
import com.yunxi.vip.service.VipDubboService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-09-06
 */
public class VipDubboServiceImpl implements VipDubboService {

    @Autowired
    @Qualifier(value = "vipServiceImpl")
    VipService vipService;

    @Autowired
    PartnerDubboService partnerDubboService;

    @Autowired
    @Qualifier("codeCreateServiceImpl")
    CodeCreateService codeCreateServiceImpl;

    @Override
    public VipVO getById(String id) throws InvocationTargetException, IllegalAccessException {
        Vip vip = vipService.getById(id);
        VipVO vipVO = new VipVO();
        BeanUtils.copyProperties(vip, vipVO);
        return vipVO;
    }

    @Override
    public ApiResult saveOrUpdate(VipVO vipVO, LoginInfo loginInfo) throws Exception {

        boolean isFirst = false;

        if(StringUtils.isEmpty(vipVO.getVipId())){
            isFirst = true;
        }

        if(org.apache.commons.lang3.StringUtils.isNotEmpty(loginInfo.getPartnerCode())){
            vipVO.setPartnerCode(loginInfo.getPartnerCode());
            vipVO.setVipType(1);
        }else{
            vipVO.setVipType(2);
        }

        if (vipVO.getPersonList() != null) {
            List list = vipVO.getPersonList();
            vipVO.setPersons(JSONArray.toJSONString(list));
        }

        if (vipVO.getBankList() != null) {
            List list = vipVO.getBankList();
            vipVO.setBanks(JSONArray.toJSONString(list));
        }

        if(StringUtils.isEmpty(vipVO.getVipNo())){
            String vipNo = codeCreateServiceImpl.createCode("D","yyMMdd",3);
            vipVO.setVipNo(vipNo);
        }

        Vip vip = new Vip();
        BeanUtils.copyProperties(vipVO, vip);
        vipService.saveOrUpdate(vip);
        if(vipVO.getVipType()==2)
            return syncPartner(vipVO, loginInfo, isFirst, vip);
        return ResponseUtil.ok();
    }

    private ApiResult syncPartner(VipVO vipVO, LoginInfo loginInfo, boolean isFirst, Vip vip) throws Exception {
        //根据大客户编号查询是否存在渠道
        PartnerVO his = partnerDubboService.getByPartnerCode(vip.getVipNo());
        //同步渠道信息
        PartnerVO pv = new PartnerVO();
        if(his!=null){
            pv.setPartnerId(his.getPartnerId());
            isFirst = false;
        }else{
            isFirst = true;
        }
        pv.setCreateBy(loginInfo.getUserCode());
        pv.setPartnerCode(vip.getVipNo());
        pv.setPartnerName(vip.getVipName());
        pv.setPartnerSimpleName(vip.getVipName());
        pv.setStatus(1);
        pv.setTenantId("pms");
        pv.setAccountGroupCode("Y010");
        pv.setAddr(vip.getVipAddr());
        pv.setProvince(vip.getProvinceCode());
        pv.setAkont("1122010000");
        pv.setParentCompanyCode("AS51");
        pv.setCountry("CN");
        pv.setCity(vip.getCityCode());
        pv.setRegion(vip.getRegionCode());
        pv.setBusinessCode("SELL");
        pv.setPostalCode(vip.getPostalCode());
        pv.setIsAfhalen("N");
        pv.setIsDirectSending("N");

        if (vipVO.getBankList() != null) {
            List list = vipVO.getBankList();
            pv.setPartnerInvoiceVOList(list);
        }

        if(isFirst){
            List<String> accountPropCodeList = new ArrayList();
            accountPropCodeList.add("XS03");
            pv.setAccountPropCodeList(accountPropCodeList);

            List<PartnerStorageVO> partnerStorageVOList = new ArrayList();
            PartnerStorageVO vo = new PartnerStorageVO();
            vo.setStorageName(vip.getVipNo()+"仓库");
            vo.setAddr(vip.getVipAddr());
            partnerStorageVOList.add(vo);
            pv.setPartnerStorageVOList(partnerStorageVOList);
        }

        //pv.setPostalCode(vip.getp);
        return partnerDubboService.saveOrUpdateByVip(pv,isFirst,loginInfo);
    }

    @Override
    public void removeById(String id) {
        vipService.removeById(id);
    }

    @Override
    public PageInfo<VipVO> findPageByVO(VipVO vipVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        PageInfo<VipVO> pageInfo = vipService.pageByVo(vipVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<VipVO> vipVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        vipService.removeByListVo(vipVOList);
    }

    @Override
    public List listByVo(VipVO vipVO) {
        return vipService.listByVo(vipVO);
    }

    @Override
    public VipVO getByVin(String vin) {
        return vipService.getByVin(vin);
    }
}
